package cn.highedu.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
@Component // 交给Spring管理
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    public MyBeanFactoryPostProcessor() {
        System.out.println("MyBeanFactoryPostProcessor的构造方法");
    }

    /**
     * postProcess 是一个后置处理器
     * 当前方法 在Spring加载所有的Bean之后执行
     * @param configurableListableBeanFactory 封装了Spring容器中所有的Bean
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println(configurableListableBeanFactory);
        // 获取所有的Bean
        String[] beanNames = configurableListableBeanFactory.getBeanDefinitionNames();
        Arrays.stream(beanNames).forEach(beanName -> System.out.println("管理的对象:" + beanName));
    }
}
